#include "flappybird.h"
#include "ui_flappybird.h"
#include <QKeyEvent>
#include <QPushButton>
#include <QPainter>
#include <QTime>
#include <QTimer>
#include <QPoint>
#include <QColor>
#include <QDebug>
#include <QMessageBox>
#include <QGraphicsItem>

flappybird::flappybird(QWidget *parent):QMainWindow(parent),ui(new Ui::flappybird)
{
    ui->setupUi(this);

    button = new QPushButton("开始游戏",this);
    button->setGeometry(500,650,200,50);
    connect(button,SIGNAL(clicked()),this,SLOT(slotStart()));
    ran1[0]=qrand()%600;
    ran1[1]=qrand()%600;
    ran1[2]=qrand()%600;
    ran1[3]=qrand()%600;
    ran2[0]=qrand()%100;
    ran2[1]=qrand()%100;
    ran2[2]=qrand()%100;
    ran2[3]=qrand()%100;

}

void flappybird::setl()
{

    if(l1<-80)
    {
        ran1[0]=qrand()%600;
        ran2[0]=qrand()%100;
        l1=1280+qrand()%30-qrand()%30;
    }
    if(l2<-80)
    {
        ran1[1]=qrand()%600;
        ran2[1]=qrand()%100;
        l2=1280+qrand()%30-qrand()%30;
    }
    if(l3<-80)
    {
        ran1[2]=qrand()%600;
        ran2[2]=qrand()%100;
        l3=1280+qrand()%30-qrand()%30;
    }
    if(l4<-80)
    {
        ran1[3]=qrand()%600;
        ran2[3]=qrand()%100;
        l4=1280+qrand()%30-qrand()%30;
    }

    update();

}

void flappybird::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    p.setBrush(Qt::blue);
    p.setFont(QFont("微软雅黑",20,700,false));
    p.setBrush(Qt::lightGray);
    p.drawRect(0,0,1200,800);
    p.drawText(QPoint(20,60),"时间: "+QString::number(time/1000)+"s");



    setl();







if(time)
{
    l1-=0.25;
    l2-=0.25;
    l3-=0.25;
    l4-=0.25;
}


p.setBrush(Qt::black);
int k1=ran1[0]-200+ran2[0];
int k2=ran1[0]-500-ran2[0];

int k3=ran1[1]-200+ran2[1];
int k4=ran1[1]-500-ran2[1];

int k5=ran1[2]-200+ran2[2];
int k6=ran1[2]-500-ran2[2];

int k7=ran1[3]-200+ran2[3];
int k8=ran1[3]-500-ran2[3];


p.drawRect(l1,0,60,k1);
p.drawRect(l1,800,60,k2);

p.drawRect(l2,0,60,ran1[1]-200+ran2[1]);
p.drawRect(l2,800,60,ran1[1]-500-ran2[1]);

p.drawRect(l3,0,60,ran1[2]-200+ran2[2]);
p.drawRect(l3,800,60,ran1[2]-500-ran2[2]);

p.drawRect(l4,0,60,ran1[3]-200+ran2[3]);
p.drawRect(l4,800,60,ran1[3]-500-ran2[3]);

    display();
    die();

    if(count==1)
    {
        F.v+=F.g;
        F.y-=F.v;
        p.setBrush(Qt::red);
        p.drawRect(180,F.y,40,40);
    }


    //上面两顶点碰到柱子
    if((F.y<k1&&180>l1&&180<l1+60&&count==1)||(F.y<k1&&220>l1&&220<l1+60&&count==1)||(F.y<k3&&180>l2&&180<l2+60&&count==1)||(F.y<k3&&220>l2&&220<l2+60&&count==1)||(F.y<k5&&180>l3&&180<l3+60&&count==1)||(F.y<k5&&220>l3&&220<l3+60&&count==1)||(F.y<k7&&180>l4&&180<l4+60&&count==1)||(F.y<k7&&220>l4&&220<l4+60&&count==1))
    {
        count=2;
        F.v=0;
        diecount=1;
        QMessageBox::about(this,"游戏结束","你的得分是"+QString::number(time/1000));
        button->setText("开始游戏");

    }

    //下面两顶点碰到柱子
    if((F.y+40-k2>800&&180>l1&&180<l1+60&&count==1)||(F.y+40-k2>800&&220>l1&&220<l1+60&&count==1)||(F.y+40-k4>800&&180>l2&&180<l2+60&&count==1)||(F.y+40-k4>800&&220>l2&&220<l2+60&&count==1)||(F.y+40-k6>800&&180>l3&&180<l3+60&&count==1)||(F.y+40-k6>800&&220>l3&&220<l3+60&&count==1)||(F.y+40-k8>800&&180>l4&&180<l4+60&&count==1)||(F.y+40-k8>800&&220>l4&&220<l4+60&&count==1))
    {
        count=2;
        F.v=0;
        diecount=1;
        QMessageBox::about(this,"游戏结束","你的得分是"+QString::number(time/1000));
        button->setText("开始游戏");

    }

}

void flappybird::keyPressEvent(QKeyEvent *event)
{
    if(!state){
        return ;
    }
    switch (event->key()) {
    case Qt::Key_A:
        F.v=0.9;
        break;

    default:
        return;//忽略其他按钮
    }
    update();

}
void flappybird::die()
{
    if((F.y<0||F.y>760)&&diecount==0)
    {
        count=2;
        F.v=0;
        diecount=1;
        QMessageBox::about(this,"游戏结束","你的得分是"+QString::number(time/1000));
        button->setText("点击或按空格开始游戏");

    }
}

void flappybird::display()
{
if(count==1)
time+=time1.restart();
update();
}
void flappybird::slotStart()
{
    time1.elapsed();
    time=0;
    l1=1200;
    l2=1600;
    l3=2000;
    l4=2400;
    ran1[0]=qrand()%600;
    ran1[1]=qrand()%600;
    ran1[2]=qrand()%600;
    ran1[3]=qrand()%600;
    ran2[0]=qrand()%100;
    ran2[1]=qrand()%100;
    ran2[2]=qrand()%100;
    ran2[3]=qrand()%100;
    F.y=400;
    F.v=0.9;
    count=1;
    diecount=0;
    time1.start();
    QMessageBox::about(this,"游戏方法","按A键进行跳跃");
    button->setText("重新游戏");
    state=true;
    update();

}


flappybird::~flappybird()
{
      delete ui;
}
